﻿using Survey.DataAccessLayer;
using Survey.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebUI.constant;
using WebUI.dto;
using WebUI.Units;

namespace WebUI.Business
{
    public class QuestionFeedbackBusiness
    {
        #region 答卷页面
        QuestionFeedbackAccessor feedbackAccessor = new QuestionFeedbackAccessor();
        QuestionnaireAccessor questionnaireAccessor = new QuestionnaireAccessor();
        //api/PostQuestionFeedback
        public Object PostQuestionFeedback(List<int> IdCollection)
        {
            foreach (var item in IdCollection)
            {
                feedbackAccessor.Add(new QuestionFeedback
                {
                    QuestionOptionId = item
                });
            }
            try
            {

                feedbackAccessor.Save();
                return Result<object>.Build(
                    CodeEnum.POST_QUESTIONFEEDBACK_SUCCESS
                    , CodeDictionary.GetCodeMsg(CodeEnum.POST_QUESTIONFEEDBACK_SUCCESS)
                    , null
                    , true);
            }
            catch (Exception)
            {
                return Result<object>.Build(
                    CodeEnum.POST_QUESTIONFEEDBACK_FAILED
                    , CodeDictionary.GetCodeMsg(CodeEnum.POST_QUESTIONFEEDBACK_FAILED)
                    , null
                    , false);
            }
            
        }
        //api/GetQuestionnaireById
        public Object GetQuestionnaireById(int id)
        {
            try
            {
                Questionnaire questionnaire = questionnaireAccessor.GetQuestionnaire(id);
                //屏蔽用户信息
                questionnaire.RefUser = null;
                questionnaire.UserId = -1;
                var result = DataProcess.Obj2Json(questionnaire);

                return Result<Questionnaire>.Build(
                    CodeEnum.SELECT_SUCCESS
                    , CodeDictionary.GetCodeMsg(CodeEnum.SELECT_SUCCESS)
                    , (Questionnaire)DataProcess.Json2Obj(result, (new Questionnaire()).GetType())
                    , true);
            }
            catch (Exception e)
            {
                return Result<Object>.Build(
                    CodeEnum.SELECT_NOTHING
                    , CodeDictionary.GetCodeMsg(CodeEnum.SELECT_NOTHING)
                    , null
                    , false);
            }
        }
        //只有id=1的管理员才有权限获取所有问卷
        //否者只能查看自己的问卷
        // api/GetAllQuestionnaire
        public Object GetAllQuestionnaire(HttpSessionStateBase session)
        {
            var curentUserId = session["curentUserId"];
            if (curentUserId == null)
                return Result<Object>.Build(
                    CodeEnum.NOT_LOGIN
                    , CodeDictionary.GetCodeMsg(CodeEnum.NOT_LOGIN)
                    , null
                    , false);
            //管理员
            if ((int)curentUserId == 1)
            {
                var result1 = questionnaireAccessor.Select(q => true).Select(qq => new { Title = qq.Title, Id = qq.Id });
                return Result<Object>.Build(
                    CodeEnum.SELECT_SUCCESS
                    , CodeDictionary.GetCodeMsg(CodeEnum.SELECT_SUCCESS)
                    , result1
                    , true);
            }
            var result = questionnaireAccessor.Select(q => q.UserId == (int)curentUserId).Select(qq => new { Title = qq.Title, Id = qq.Id });
            return Result<Object>.Build(
                CodeEnum.SELECT_SUCCESS
                , CodeDictionary.GetCodeMsg(CodeEnum.SELECT_SUCCESS)
                , result
                , true);
        }


        //api/StatisticInfo
        public Object StatisticInfo(int id)
        {
            QuestionnaireAccessor questionnaireAccessor = new QuestionnaireAccessor();
            var questinnaire = questionnaireAccessor.GetQuestionnaire(id);
            var quetionCollection = questinnaire.QuestionCollection;//问卷里面的问题集合
            QuestionAccessor questionAccessor = new QuestionAccessor();

            foreach (var quetion in quetionCollection)
            {

                 var feedbackCounts = questionAccessor.GetFeedbackCount(quetion.Id);//获取问题的选项的统计信息
                foreach (var feedback in feedbackCounts)
                {
                    var option = quetion.QuestionOptionCollection.Where(op=>op.Id==feedback.Key).First();
                    option.Count = feedback.Value;
                }
               
            }
            //去除敏感信息
            questinnaire.RefUser = null;
            questinnaire.UserId = -1;
            questinnaire.CreatedAt = null;

           String result =  DataProcess.Obj2Json(questinnaire);

           return Result<Questionnaire>.Build(
                CodeEnum.SELECT_SUCCESS
                , CodeDictionary.GetCodeMsg(CodeEnum.SELECT_SUCCESS)
                , (Questionnaire)DataProcess.Json2Obj(result,(new Questionnaire()).GetType())
                , true);
        }
        #endregion
    }
}